/**
 * 
 */
package com.hfjyz.examination.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import com.hfjyz.examination.command.QuestionEditCommand;
import com.hfjyz.examination.dao.QuestionCategoryDao;
import com.hfjyz.examination.dao.QuestionDao;
import com.hfjyz.examination.dao.QuestionOptionsDao;
import com.hfjyz.examination.domain.Question;
import com.hfjyz.examination.domain.QuestionCategory;
import com.hfjyz.examination.domain.QuestionOptions;
import com.hfjyz.examination.exception.HfjyzException;
import com.hfjyz.examination.service.QuestionService;

/**
 *
 */
public class QuestionServiceImpl implements QuestionService {

	private QuestionDao questionDao;
	private QuestionOptionsDao questionOptionsDao;
	private QuestionCategoryDao questionCategoryDao;

	@Override
	public void editOk(HttpServletRequest request, QuestionEditCommand command,
			BindException errors) throws Exception {
		Question question = null;
		if (command.getId() != null) {
			question = questionDao.get(command.getId());
			if (question == null) {
				throw new HfjyzException("error.question.edit.alreadydelete");
			}
		} else {
			question = new Question();
		}
		question.setContent(command.getContent());
		question.setKeydesc(command.getKeydesc());
		Date now = Calendar.getInstance().getTime();
		question.setStatus("1");
		question.setQfrom(1);
		question.setQkey(command.getQkey());
		question.setQlevel(3);
		question.setQtype(command.getQtype());
		question.setCdate(now);
		question.setQuestionCategory(new QuestionCategory(command.getCategory()
				.getId(), now));
		if (command.getId() == null) {
			Long pk = questionDao.add(question);
			command.setId(pk);
		} else {
			questionDao.update(question);
		}
		if (command.getQuestionOptionses().size() > 0) {
			Set<QuestionOptions> ops = new HashSet<QuestionOptions>();
			for (QuestionOptions op : command.getQuestionOptionses()) {
				op.setQuestion(question);
				ops.add(op);
			}
			questionOptionsDao.updateAll(ops);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Map initEdit(HttpServletRequest request,
			QuestionEditCommand command, Errors errors) throws Exception {
		Map model = new HashMap();
		model.put("categories", questionCategoryDao.listAll());
		if (command.getId() != null) {
			Question category = questionDao.get(command.getId());
			command.setQuestion(category);
			command.setCategory(category.getQuestionCategory());
		} else {
			List<QuestionOptions> qos = new ArrayList<QuestionOptions>();
			Date now = Calendar.getInstance().getTime();
			qos.add(new QuestionOptions(1L, command.getQuestion(), "A",
					"testA", now));
			qos.add(new QuestionOptions(1L, command.getQuestion(), "B",
					"testB", now));
			qos.add(new QuestionOptions(1L, command.getQuestion(), "C",
					"testC", now));
			qos.add(new QuestionOptions(1L, command.getQuestion(), "D",
					"testD", now));
			command.setQuestionOptionses(qos);
			command.setQtype(1);
		}
		return model;
	}

	/**
	 * @return the questionCategoryDao
	 */
	public QuestionDao getQuestionDao() {
		return questionDao;
	}

	/**
	 * @param questionCategoryDao
	 *            the questionCategoryDao to set
	 */
	public void setQuestionDao(QuestionDao dao) {
		this.questionDao = dao;
	}

	/**
	 * @return the questionOptionsDao
	 */
	public QuestionOptionsDao getQuestionOptionsDao() {
		return questionOptionsDao;
	}

	/**
	 * @param questionOptionsDao
	 *            the questionOptionsDao to set
	 */
	public void setQuestionOptionsDao(QuestionOptionsDao questionOptionsDao) {
		this.questionOptionsDao = questionOptionsDao;
	}

	/**
	 * @return the questionCategoryDao
	 */
	public QuestionCategoryDao getQuestionCategoryDao() {
		return questionCategoryDao;
	}

	/**
	 * @param questionCategoryDao the questionCategoryDao to set
	 */
	public void setQuestionCategoryDao(QuestionCategoryDao questionCategoryDao) {
		this.questionCategoryDao = questionCategoryDao;
	}

}
